#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

const int maxn=5000;
int n, f[2][6][maxn+2];
char s[maxn+2], tab[6][4]={"CAB", "BAC", "CBA", "ABC", "BCA", "ACB"};

int main() {
    freopen("letter.in", "r", stdin);
    freopen("letter.out", "w", stdout);

    scanf("%s", s+1);
    n=strlen(s+1);

    memset(f, 127, sizeof f);
    for (int i=0; i<6; i++) f[0][i][0] = 0;
    for (int i=1; i<=n; i++) {
        int t=i%2;
        memset(f[t], 127, sizeof f[t]);
        for (int j=0; j<6; j++) {
            for (int l=1; l<=n; l++) {
                if (tab[j][l%3]==s[i]) {
                    for (int x=max(l-2, 0); x<=l+2; x++) {
                        if (f[!t][j][x]<=3*n) {
                            f[t][j][l] = min(f[t][j][l], f[!t][j][x]+abs(x-l)+1);
                        }
                    }
                    f[t][j][0] = min(f[t][j][0], f[t][j][l]+l);
                }
            }
            for (int k=0; k<6; k++) {
                if (tab[j][1]==tab[k][1]) {
                    f[t][k][1] = min(f[t][k][1], f[t][j][1]);
                    if (tab[j][2]==tab[k][2]) {
                        f[t][k][2] = min(f[t][k][2], f[t][j][2]);
                    }
                }
                f[t][k][0] = min(f[t][k][0], f[t][j][0]);
            }
        }
    }

    printf("%d\n", f[n%2][0][0]);

    fclose(stdin);
    fclose(stdout);
    return 0;
}
